import { companionFeedbackListApi } from '@/api/pcs/companionFeedback'
import { defaultPageSize } from '@/utils/antdHelper'
import { formatDate } from '@/utils/displayHelper'
import modelExtend from '@/utils/dvaUtil/modelExtend'

export const namespace = 'companionFeedback'

const initialState = {
  searchParams: {},
  pagination: {
    current: 1,
    pageSize: defaultPageSize,
    total: 0,
  },
  pageList: [],
}

export default modelExtend({
  namespace,
  state: initialState,
  matchedActions: toAction => {
    toAction('getPageList')
  },
  effects: {
    *getPageList({ payload }, { call, update, select }) {
      const { searchParams, pagination } = yield select(state => state[namespace])
      const params = {
        ...searchParams,
        ...pagination,
        ...payload,
      }

      if (params.createTime?.length) {
        params.startCreateTime = formatDate(params.createTime[0])
        params.endCreateTime = formatDate(params.createTime[1])
        delete params.createTime
      }

      const { data } = yield call(companionFeedbackListApi, params)
      const { content, current, pageSize, total } = data
      yield update({ pageList: content, pagination: { current, pageSize, total } })
    },
  },
})
